# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Maxim Karasev <begs@disroot.org>
# Maintainer: Maxim Karasev <begs@disroot.org>
pkgname=fpc-stage0
pkgver=3.2.2
pkgrel=1
pkgdesc="Free Pascal Compiler binary for bootstrapping"
url="http://freepascal.org"
arch="x86_64 aarch64 armv7 armhf ppc64le x86"
license="GPL-2.0-or-later AND LGPL-2.1-or-later"
_fpcarchs="x86_64 aarch64 arm powerpc64le i386"
for _fpcarch in $_fpcarchs; do
	source="$source
	https://downloads.sourceforge.net/sourceforge/freepascal/Linux/$pkgver/fpc-$pkgver.$_fpcarch-linux.tar"
done
case "$CARCH" in
	x86_64|aarch64) _fpcarch="$CARCH";;
	armv7|armhf) _fpcarch=arm;;
	ppc64le) _fpcarch=powerpc64;;
	x86) _fpcarch=i386;;
esac
options="!check" # no tests
builddir="$srcdir/fpc-$pkgver.$_fpcarch-linux"

# Provide fpc-bootstrap for testing/cyclone.
provides="fpc-bootstrap"
provider_priority=1 # lowest

package() {
	echo "$CARCH"
	tar xf binary.$_fpcarch-linux.tar

	# Unpack only files strictly necessary for build
	tar xf base.$_fpcarch-linux.tar.gz
	tar xf utils-fpcm.$_fpcarch-linux.tar.gz

	install -Dm755 bin/* -t "$pkgdir"/usr/bin/

	cp -r lib "$pkgdir"/usr

	# Create a symlink needed for fpc to find appropriate binary
	ln -s /usr/lib/fpc/"$pkgver"/"$(find "$pkgdir"/usr/lib/fpc/"$pkgver"\
		-maxdepth 1 -executable -name "ppc*" | head -n1 |
			xargs basename)" "$pkgdir"/usr/bin
}

sha512sums="
58b8722e65e2c466bd20d6aeedbd675c733ec144bb91c562f7c2f2bc04ec8696c526bef6804f10e554961b2342562d484e0dbea6440aef0c605593dfac826729  fpc-3.2.2.powerpc64le-linux.tar
534df830a951824525044479ded23ef1e61b104c0dd65c837ad0d8d9ced9eb7349925a3f759756aa95ae17e567d8b1cd5c7e2092d3c665eaac578845b48aa467  fpc-3.2.2.x86_64-linux.tar
b4f1a94d1a05901f4ffc2e07519e9b03666850211e571cbce33f0ba16d8a1d88de256c8e111579f46d56f157630dfc722a8ae9ce2b200fc784c588ea4e6bd5df  fpc-3.2.2.i386-linux.tar
8a52c53cd167af39222296db7c91aadf0b5d845272833a961c5db0c7d2d4cb5e61f41f71e0989c193dc2923770440b19d7d3d71de65291d0a3e0a15edc793316  fpc-3.2.2.aarch64-linux.tar
7dd3435e74d2e5ec45fe5ca9a5d621ababe9ac0e9131c73a184686fb97892f64358157bfa92e5238f5b16eaf9f9198dffbbddd58d5d04b4a2d6a63cfdc16a491  fpc-3.2.2.arm-linux.tar
"
